package com.algorithm.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 无向图最短路径 floyd
 * https://blog.51cto.com/u_15349906/5089122
 */
public class UnDirectedGraphMinRoute {
    public static void main(String[] args) {
        //测试看看图是否创建成功
        char[] vertex = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int[][] matrix = new int[vertex.length][vertex.length];
        final int N = 65535;
        matrix[0] = new int[]{0, 5, 7, N, N, N, 2};
        matrix[1] = new int[]{5, 0, N, 9, N, N, 3};
        matrix[2] = new int[]{7, N, 0, N, 8, N, N};
        matrix[3] = new int[]{N, 9, N, 0, N, 4, N};
        matrix[4] = new int[]{N, N, 8, N, 0, 5, 4};
        matrix[5] = new int[]{N, N, N, 4, 5, 0, 6};
        matrix[6] = new int[]{2, 3, N, N, 4, 6, 0};
        Graph graph = new Graph(vertex.length, matrix, vertex);
        graph.setVertex(vertex);
        // 调用弗洛伊德算法
        graph.floyd();

        graph.show(0,3);
    }

    static class Graph {
        private char[] vertex;  // 存放顶点数组
        private int[][] dis;    // 保存从各个顶点出发到其他顶点的距离，最后的结果，也是保存在该数组
        private int[][] pre;    // 保存到达目标顶点的前驱结点

        public char[] getVertex() {
            return vertex;
        }

        public void setVertex(char[] vertex) {
            this.vertex = vertex;
        }

        public int[][] getDis() {
            return dis;
        }

        public void setDis(int[][] dis) {
            this.dis = dis;
        }

        public int[][] getPre() {
            return pre;
        }

        public void setPre(int[][] pre) {
            this.pre = pre;
        }

        /**
         * 构造器
         *
         * @param length 长度大小
         * @param matrix 初始邻接矩阵
         * @param vertex 顶点数组
         */
        public Graph(int length, int[][] matrix, char[] vertex) {
            this.vertex = vertex;
            this.dis = matrix;
            this.pre = new int[length][length];
            // 对pre数组初始化,存的的是
            for (int i = 0; i < length; i++) {
                Arrays.fill(pre[i], i);
            }
        }

        //通过node名称获取node下标
        public int getNodeIndex(char[] vertex, char nodeName){
            for(int i=0;i<vertex.length;i++){
                if(vertex[i]==nodeName){
                    return i;
                }
            }
            return -1;
        }

        public List<Character> getRoute(int[] currentPreArr, int startIndex, int endIndex){
            List<Character> routeList = new ArrayList<>();
            List<Character> list = new ArrayList<>();
            int i = endIndex;
            list.add(vertex[i]);
            while (i != startIndex){
                i = currentPreArr[i];
                list.add(vertex[i]);
            }
            for(int j=list.size()-1;j>=0; j--){
                routeList.add(list.get(j));
                if(j!=0)
                    System.out.print(list.get(j)+"->");
                else System.out.print(list.get(j));
            }

            return routeList;
        }


        // 显示pre数组和dis数组{
        public void show(int start, int end) {
            // 为了显示便于阅读
            for (int i = 0; i < dis.length; i++) {
                if(start<0 || start>=dis.length ||
                        end<0 || end>=dis.length ||
                        start>end){
                    continue;
                }
                if(i!=start){
                    continue;
                }
                // 先将pre数组输出
//                for (int j = 0; j < dis.length; j++) {
//                    System.out.print(vertex[pre[i][j]] + " ");
//                }
//                System.out.println();

                int[] currentPreArr = pre[i];

                int startIndex = getNodeIndex(vertex, vertex[i]);
                // 将dist组输出
                for (int j = 0; j < dis.length; j++) {
                    if(j!=end){
                        continue;
                    }

                    //打印最短路径以及 最小值
                    int endIndex = getNodeIndex(vertex, vertex[j]);
                    List<Character> routeList = getRoute(currentPreArr, startIndex, endIndex);
                    //System.out.println("(" + vertex[i] + "->" + vertex[j] + " " + dis[i][j] + ") ");
                    System.out.println(" " + dis[i][j]);
                }
                System.out.println();
                System.out.println();
            }
        }

        public void floyd() {
            int len = 0;    // 记录变量保存距离
            // 对中间顶点遍历，k就是中间顶点的下标 ['A','B','C','D','E','F','G']
            for (int k = 0; k < dis.length; k++) {
                // 从i顶点开始出发 ['A','B','C','D','E','F','G']
                for (int i = 0; i < dis.length; i++) {
                    // 到达j顶点，['A','B','C','D','E','F','G']
                    for (int j = 0; j < dis.length; j++) {
                        len = dis[i][k] + dis[k][j];    // => 求出从i顶点出发，经过k中间顶点，到达j顶点距离
                        if (len < dis[i][j]) {  // 如果len小于dis[i][j]
                            dis[i][j] = len;    // 更新距离
                            pre[i][j] = pre[k][j];  // 更新前驱结点
                        }
                    }
                }
            }
        }
    }
}


